//
//  LYNewFeatureViewController.swift
//  Sina_Weibo2
//
//  Created by Elsevier on 16/5/17.
//  Copyright © 2016年 Yang Lu. All rights reserved.
//

import UIKit

private let reuseIdentifier = "cell"
private let ItemCount = 4

class LYNewFeatureViewController: UICollectionViewController {

    
    private let layout = UICollectionViewFlowLayout()
    
    init(){
        super.init(collectionViewLayout: layout)
    }
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    
    override func viewDidLoad() {
        super.viewDidLoad()
        
       prepareCollectionView()
    }
    
    private func prepareCollectionView(){
        
        //注册Cell
        self.collectionView!.registerClass(LYNewFeatureViewController.self, forCellWithReuseIdentifier: reuseIdentifier)
        layout.itemSize = UIScreen.mainScreen().bounds.size
        layout.scrollDirection = UICollectionViewScrollDirection.Horizontal
        //行间距
        layout.minimumLineSpacing = 0
        //Item间距
        layout.minimumInteritemSpacing = 0
        //取消弹簧效果
        collectionView?.bounces = false
        // 不要水平滚动条
        //        collectionView?.showsHorizontalScrollIndicator = false
        // 分页
        collectionView?.pagingEnabled = true
        
    }
}

extension LYNewFeatureViewController{
    
    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return ItemCount
    }
    
    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! LYNewFeatureCell
        cell.backgroundColor = UIColor .darkGrayColor()
        cell.index = indexPath.item
        return cell
    }
    
    override func scrollViewDidEndDecelerating(scrollView: UIScrollView) {
        let index = Int(scrollView.contentOffset.x / scrollView.frame.width)
        LYPrint(items: "scrollView停止滚动,在第\(index)页")
        
        if index == ItemCount - 1{
            let indexPath = NSIndexPath(forItem: index, inSection: 0)
            let cell = collectionView?.cellForItemAtIndexPath(indexPath) as! LYNewFeatureCell
            cell.startAnimation()
        }
    }
}
